import {postAction, getAction, getActionLogin} from "@/utils/manage";
import {
    PHONE_LOGIN,
    EMAIL_LOGIN,
    USER_DETAIL,
    LOGIN_QR_CHECK,
    LOGIN_QR_CREATE,
    LOGIN_QR_KEY,
    LOGIN_STATUS,
    USER_ACCOUNT,
    LOGIN_REFRESH,
    SEARCH_DEFAULT,
    SEARCH,
    CLOUD_SEARCH,
    SONG_URL,
    PERSONALIZED,
    USER_PLAYLIST,
    PLAYLIST_DETAIL,
    PLAYLIST_TRACK_ALL,
    LIKELIST,
    RECOMMEND_RESOURCE,
    RECOMMEND_SONGS, HISTORY_RECOMMEND_SONGS_DETAIL,
} from "@/api/interface";


const base_url = 'http://localhost:3000';
// 登录
export const login = (loginType, params) => {
    if (loginType == 'PHONE') {
        return postAction(base_url + PHONE_LOGIN, params);
    } else if (loginType == 'EMAIL') {
        return postAction(base_url + EMAIL_LOGIN, params);
    } else if (loginType == 'qr') {
        return qrLogin(params);
    }
}

// 二维码登录
async function qrLogin() {
    let params = {}
    let res = await getActionLogin(base_url + LOGIN_QR_KEY + `?timerstamp=${Date.now()}`, params);
    let key = res.data.unikey;
    let res2 = await getActionLogin(base_url + LOGIN_QR_CREATE + `?key=${key}&qrimg=true&timerstamp=${Date.now()}`, params);
    let result = {
        'key': key,
        'qrimg': res2.data.qrimg
    }
    return result;
}


export const loginRefresh = (params) => getAction(base_url + LOGIN_REFRESH, params)
export const checkStatus = (key) => getActionLogin(base_url + LOGIN_QR_CHECK + `?key=${key}&timerstamp=${Date.now()}`);
// 调用此接口,可获取登录状态
export const loginStatus = (params) => getActionLogin(base_url + LOGIN_STATUS + `?timerstamp=${Date.now()}`, params);

// 获取用户详情
export const userDetail = (params) => getAction(base_url + USER_DETAIL, params);

// 获取账号信息
export const userAccount = (params) => getAction(base_url + USER_ACCOUNT, params);
// 默认搜索关键词
export const searchDefault = (params) => getAction(base_url + SEARCH_DEFAULT, params);
// 搜索
export const search = (params) => getAction(base_url + SEARCH, params);
//搜索 (更全)
export const cloudSearch = (params) => getAction(base_url + CLOUD_SEARCH, params);
// 获取音乐 url
export const songUrl = (params) => getAction(base_url + SONG_URL, params);

// 推荐歌单
export const personalized = (params) => getAction(base_url + PERSONALIZED, params);
// 获取用户歌单
export const userPlaylist = (params) => getAction(base_url + USER_PLAYLIST, params);
// 获取歌单详情
export const playlistDetail = (params) => getAction(base_url + PLAYLIST_DETAIL, params);
// 获取歌单所有音乐
export const playlistTrackAll =  (params) => getAction(base_url + PLAYLIST_TRACK_ALL, params);
// 喜欢音乐列表
export const likelist =  (params) => getAction(base_url + LIKELIST, params);
// 获取每日推荐歌单
export const recommendResource =  (params) => getAction(base_url + RECOMMEND_RESOURCE, params);
// 获取每日推荐歌曲（需要登录）
export const recommendSongs =  (params) => getAction(base_url + RECOMMEND_SONGS, params);
// 获取历史日推详情数据
export const historyRecommendSongsDetail =  (params) => getAction(base_url + HISTORY_RECOMMEND_SONGS_DETAIL, params);

